home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / comm / tcp / curl-711.lha / curl-7.1.1 / TheArtOfHttpScripting < prev    next >
Encoding:
Text File  |  2000-08-11  |  13.9 KB  |  346 lines

  1. Author:  Daniel Stenberg <daniel@haxx.se>
  2. Date:    August 7, 2000
  3. Version: 0.2
  4.  
  5.                 The Art Of Scripting HTTP Requests Using Curl
  6.                 =============================================
  7.  
  8.  This document will assume that you're familiar with HTML and general
  9.  networking.
  10.  
  11.  The possibility to write scripts is essential to make a good computer
  12.  system. Unix' capability to be extended by shell scripts and various tools to
  13.  run various automated commands and scripts is one reason why it has succeeded
  14.  so well.
  15.  
  16.  The increasing amount of applications moving to the web has made "HTTP
  17.  Scripting" more frequently requested and wanted. To be able to automatically
  18.  extract information from the web, to fake users, to post or upload data to
  19.  web servers are all important tasks today.
  20.  
  21.  Curl is a command line tool for doing all sorts of URL manipulations and
  22.  transfers, but this particular document will focus on how to use it when
  23.  doing HTTP requests for fun and profit. I'll assume that you know how to
  24.  invoke 'curl --help' or 'curl --manual' to get basic information about it.
  25.  
  26.  Curl is not written to do everything for you. It makes the requests, it gets
  27.  the data, it sends data and it retrieves the information. You probably need
  28.  to glue everything together using some kind of script language or repeated
  29.  manual invokes.
  30.  
  31. 1. The HTTP Protocol
  32.  
  33.  HTTP is the protocol used to fetch data from web servers. It is a very simple
  34.  protocol that is built upon TCP/IP. The protocol also allow information to
  35.  get sent to the server from the client using a few different methods, as will
  36.  be shown here.
  37.  
  38.  HTTP is plain ASCII text lines being sent by the client to a server to
  39.  request a particular action, and then the server replies a few text lines
  40.  before the actual requested content is sent to the client.
  41.  
  42.  Using curl's option -v will display what kind of commands curl sends to the
  43.  server, as well as a few other informational texts. -v is the single most
  44.  useful option when it comes to debug or even understand the curl<->server
  45.  interaction.
  46.  
  47. 2. URL
  48.  
  49.  The Uniform Resource Locator format is how you specify the address of a
  50.  particular resource on the internet. You know these, you've seen URLs like
  51.  http://curl.haxx.se or https://yourbank.com a million times.
  52.  
  53. 3. GET a page
  54.  
  55.  The simplest and most common request/operation made using HTTP is to get a
  56.  URL. The URL could itself refer to a web page, an image or a file. The client
  57.  issues a GET request to the server and receives the document it asked for.
  58.  If you isse the command line
  59.  
  60.         curl http://curl.haxx.se
  61.  
  62.  you get a web page returned in your terminal window. The entire HTML document
  63.  that that URL holds.
  64.  
  65.  All HTTP replies contain a set of headers that are normally hidden, use
  66.  curl's -i option to display them as well as the rest of the document. You can
  67.  also ask the remote server for ONLY the headers by using the -I option.
  68.  
  69. 4. Forms
  70.  
  71.  Forms are the general way a web site can present a HTML page with fields for
  72.  the user to enter data in, and then press some kind of 'OK' or 'submit'
  73.  button to get that data sent to the server. The server then typically uses
  74.  the posted data to decide how to act. Like using the entered words to search
  75.  in a database, or to add the info in a bug track system, display the entered
  76.  address on a map or using the info as a login-prompt verifying that the user
  77.  is allowed to see what it is about to see.
  78.  
  79.  Of course there has to be some kind of program in the server end to receive
  80.  the data you send. You cannot just invent something out of the air.
  81.  
  82.  4.1 GET
  83.  
  84.   A GET-form uses the method GET, as specified in HTML like:
  85.  
  86.         <form method="GET" action="junk.cgi">
  87.           <input type=text name="birthyear">
  88.           <input type=submit name=press value="OK">
  89.         </form>
  90.  
  91.   In your favourite browser, this form will appear with a text box to fill in
  92.   and a press-button labeled "OK". If you fill in '1905' and press the OK
  93.   button, your browser will then create a new URL to get for you. The URL will
  94.   get "junk.cgi?birthyear=1905&press=OK" appended to the path part of the
  95.   previous URL.
  96.  
  97.   If the original form was seen on the page "www.hotmail.com/when/birth.html",
  98.   the second page you'll get will become
  99.   "www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK".
  100.  
  101.   Most search engines work this way.
  102.  
  103.   To make curl do the GET form post for you, just enter the expected created
  104.   URL:
  105.  
  106.         curl "www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK"
  107.  
  108.  4.2 POST
  109.  
  110.   The GET method makes all input field names get displayed in the URL field of
  111.   your browser. That's generally a good thing when you want to be able to
  112.   bookmark that page with your given data, but it is an obvious disadvantage
  113.   if you entered secret information in one of the fields or if there are a
  114.   large amount of fields creating a very long and unreadable URL.
  115.  
  116.   The HTTP protocol then offers the POST method. This way the client sends the
  117.   data separated from the URL and thus you won't see any of it in the URL
  118.   address field.
  119.  
  120.   The form would look very similar to the previous one:
  121.  
  122.         <form method="POST" action="junk.cgi">
  123.           <input type=text name="birthyear">
  124.           <input type=submit name=press value="OK">
  125.         </form>
  126.  
  127.   And to use curl to post this form with the same data filled in as before, we
  128.   could do it like:
  129.  
  130.         curl -d "birthyear=1905&press=OK" www.hotmail.com/when/junk.cgi
  131.  
  132.   This kind of POST will use the Content-Type
  133.   application/x-www-form-urlencoded and is the most widly used POST kind.
  134.  
  135.  4.3 FILE UPLOAD POST
  136.  
  137.   Back in late 1995 they defined a new to post data over HTTP. It was
  138.   documented in the RFC 1867, why this method sometimes are refered to as
  139.   a rfc1867-posting.
  140.  
  141.   This method is mainly designed to better support file uploads. A form that
  142.   allows a user to upload a file could be written like this in HTML:
  143.  
  144.     <form method="POST" enctype='multipart/form-data' action="upload.cgi">
  145.       <input type=file name=upload>
  146.       <input type=submit name=press value="OK">
  147.     </form>
  148.  
  149.   This clearly shows that the Content-Type about to be sent is
  150.   multipart/form-data.
  151.  
  152.   To post to a form like this with curl, you enter a command line like:
  153.  
  154.         curl -F upload=@localfilename -F press=OK [URL]
  155.  
  156.  4.4 HIDDEN FIELDS
  157.  
  158.   A very common way for HTML based application to pass state information
  159.   between pages is to add hidden fields to the forms. Hidden fields are
  160.   already filled in, they aren't displayed to the user and they get passed
  161.   along just as all the other fields.
  162.  
  163.   A similar example form with one visible field, one hidden field and one
  164.   submit button could look like:
  165.  
  166.     <form method="POST" action="foobar.cgi">
  167.       <input type=text name="birthyear">
  168.       <input type=text name="person" value="daniel">
  169.       <input type=submit name="press" value="OK">
  170.     </form>
  171.  
  172.   To post this with curl, you won't have to think about if the fields are
  173.   hidden or not. To curl they're all the same:
  174.  
  175.         curl -d "birthyear=1905&press=OK&person=daniel" [URL]
  176.  
  177. 5. PUT
  178.  
  179.  The perhaps best way to upload data to a HTTP server is to use PUT. Then
  180.  again, this of course requires that someone put a program or script on the
  181.  server end that knows how to receive a HTTP PUT stream.
  182.  
  183.  Put a file to a HTTP server with curl:
  184.  
  185.         curl -t uploadfile www.uploadhttp.com/receive.cgi
  186.  
  187. 6. AUTHENTICATION
  188.  
  189.  Authentication is the ability to tell the server your username and password
  190.  so that it can verify that you're allowed to do the request you're doing. The
  191.  basic authentication used in HTTP is *plain* *text* based, which means it
  192.  sends username and password only slightly obfuscated, but still fully
  193.  readable by anyone that sniffs on the network between you and the remote
  194.  server.
  195.  
  196.  To tell curl to use a user and password for authentication:
  197.  
  198.         curl -u name:password www.secrets.com
  199.  
  200.  Sometimes your HTTP access is only available through the use of a HTTP
  201.  proxy. This seems to be especially common at various companies. A HTTP proxy
  202.  may require its own user and password to allow the client to get through to
  203.  the internet. To specify those with curl, run something like:
  204.  
  205.         curl -U proxyuser:proxypassword curl.haxx.se
  206.  
  207.  If you use any one these user+password options but leave out the password
  208.  part, curl will prompt for the password interactively.
  209.  
  210.  Do note that when a program is run, its parameters are possible to see when
  211.  listing the running processes of the system. Thus, other users may be able to
  212.  watch your passwords if you pass them as plain command line options.
  213.  
  214. 7. REFERER
  215.  
  216.  A HTTP request has the ability to feature a 'referer' field, which can be
  217.  used to tell which URL that causes the client to get this particular
  218.  resource. Some programs/scripts check the referer field of requests to verify
  219.  that this wasn't arriving from an external site or unknown page. While this
  220.  is a stupid way to check something so easily forged, many scripts still do
  221.  it. Using curl, you can put anything you want in the referer-field and thus
  222.  more easily being able to fool the server into serving your request.
  223.  
  224.  Use curl to set the referer field with:
  225.  
  226.         curl -e http://curl.haxx.se daniel.haxx.se
  227.  
  228. 8. USER AGENT
  229.  
  230.  Very similar to the referer field, all HTTP requests may set the User-Agent
  231.  field. It names what user agent (client) that is being used. Many
  232.  applications use this information to decide how to display pages. Silly web
  233.  programmers try to make different pages for users of different browsers to
  234.  make them look the best possible for their particular browsers. They usually
  235.  also do different kinds of javascript, vbscript etc.
  236.  
  237.  At times, you will see that getting a page with curl will not return the same
  238.  page that you see when getting the page with your browser. Then you know it
  239.  is time to set the User Agent field to fool the server into thinking you're
  240.  one of those browsers.
  241.  
  242.  To make curl look like Internet Explorer on a Windows 2000 box:
  243.  
  244.         curl -A "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" [URL]
  245.  
  246.  Or why not look like you're using Netscape 4.73 on a Linux (PIII) box:
  247.  
  248.         curl -A "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" [URL]
  249.  
  250. 9. REDIRECTS
  251.  
  252.  When a resource is requested from a server, the reply from the server may
  253.  include a hint about where the browser should go next to find this page, or a
  254.  new page keeping newly generated output. The header that tells the browser
  255.  to redirect is Location:.
  256.  
  257.  Curl does not follow Location: headers by default, but will simply display
  258.  such pages in the same manner it display all HTTP replies. It does however
  259.  feature an option that will make it attempt to follow the Location: pointers.
  260.  
  261.  To tell curl to follow a Location: 
  262.  
  263.         curl -L www.sitethatredirects.com
  264.  
  265.  If you use curl to POST to a site that immediately redirects you to another
  266.  page, you can safely use -L and -d/-F together. Curl will only use POST in
  267.  the first request, and then revert to GET in the following operations.
  268.  
  269. 10. COOKIES
  270.  
  271.  The way the web browsers do "client side state control" is by using
  272.  cookies. Cookies are just names with associated contents. The cookies are
  273.  sent to the client by the server. The server tells the client for what path
  274.  and host name it wants the cookie sent back, and it also sends an expiration
  275.  date and a few more properties.
  276.  
  277.  When a client communicates with a server with a name and path as previously
  278.  specified in a received cookie, the client sends back the cookies and their
  279.  contents to the server, unless of course they are expired.
  280.  
  281.  Many applications and server use this method to connect a series of request
  282.  into a single logical session. To be able to use curl in such occations, we
  283.  must be able to record and send back cookies in the way that the web
  284.  application expects them. The same way browsers deal with them.
  285.  
  286.  The simplest way to send a few cookies to the server when getting a page with
  287.  curl is to add them on the command line like:
  288.  
  289.         curl -b "name=Daniel" www.cookiesite.com
  290.  
  291.  
  292.  Cookies are sent as common HTTP headers. This is practical as it allows curl
  293.  to record cookies simply by recording headers. Record cookies with curl by
  294.  using the -D option like:
  295.  
  296.         curl -D headers_and_cookies www.cookiesite.com
  297.  
  298.  Curl has a full blown cookie parsing engine built-in that comes to use if you
  299.  want to reconnect to a server and use cookies that were stored from a
  300.  previous connection (or handicrafted manually to fool the server into
  301.  believing you had a previous connection). To use previously stored cookies,
  302.  you run curl like:
  303.  
  304.         curl -b stored_cookies_in_file www.cookiesite.com
  305.  
  306. 11. HTTPS
  307.  
  308.  There are a few ways to do secure HTTP transfers. The by far most common
  309.  protocol for doing this is what is generally known as HTTPS, HTTP over
  310.  SSL. SSL encrypts all the data that is send and received over the network and
  311.  thus makes it harder for attackers to spy on sensitive information.
  312.  
  313.  SSL (or TLS as the latest version of the standard is called) offers a
  314.  truckload of advanced features to allow all those encryptions and key
  315.  infrastructure mechanisms ecnrypted HTTP requires.
  316.  
  317.  Curl supports enscrypted fetches thanks to the freely available OpenSSL
  318.  libraries. To get a pafe from a https server, simply run curl like:
  319.  
  320.         curl https://that.secure.server.com
  321.  
  322.  11.1 CERTIFICATES
  323.  
  324.   In the HTTPS world, you use certificates to validate that you are the one
  325.   you you claim to be, as an addition to normal passwords. Curl supports
  326.   client-side certificates. All certificates are locked with a PIN-code, why
  327.   you need to enter the unlock-code before the certificate can be used by
  328.   curl. The PIN-code can be specified on the command line or if not, entered
  329.   interactively when curl queries for it. Use a certificate with curl on a
  330.   https server like:
  331.  
  332.         curl -E mycert.pem https://that.secure.server.com
  333.  
  334. 12. REFERENCES
  335.  
  336.  RFC 2616 is a must to read if you want in-depth understanding of the HTTP
  337.  protocol.
  338.  
  339.  RFC 2396 explains the URL syntax
  340.  
  341.  RFC 2109 defines how cookies are supposed to work.
  342.  
  343.  http://www.openssl.org is the home of the OpenSSL project
  344.  
  345.  http://curl.haxx.se is the home of the cURL project
  346.